package com.chongbangbang.controller.user;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.chongbangbang.pojo.UserInfo;
import com.chongbangbang.pojo.UserInfo;
import com.chongbangbang.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author TP
 * @since 2022-03-16 15:20:38
 */
@RestController
@RequestMapping("/chong/user/userInfo")
@Api(tags = "user info", value = "user基本信息")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation("查询所有")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public List<UserInfo> get() {
        return userInfoService.list();
    }


    @ApiOperation("根据ID查询")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public UserInfo getById(@PathVariable Integer id) throws UnsupportedEncodingException {
        UserInfo userInfo = userInfoService.getById(id);
        if (userInfo != null) {
            if (!StringUtils.isEmpty(userInfo.getUsername())) {
                // 出库后，使用 Base64 解码
                String username = new String(Base64.decodeBase64(userInfo.getUsername().getBytes()), "utf-8");
                userInfo.setUsername(username);
            }
        }

        return userInfo;
    }


    @ApiOperation("单个插入")
    @PostMapping("")
    public boolean post(UserInfo userInfo) throws UnsupportedEncodingException {
        System.out.println("\n\n"+ userInfo + "\n");
        // 入库之前，使用 Base64 编码
        if (!StringUtils.isEmpty(userInfo.getUsername())) {
            String username = userInfo.getUsername();
            username = Base64.encodeBase64String(username.getBytes("utf-8"));
            userInfo.setUsername(username);
        }
        return userInfoService.save(userInfo);
    }


    @ApiOperation("批量插入")
    @RequestMapping(value = "/_mcreate", method = RequestMethod.POST)
    public boolean postBatches(@ApiParam(value = "传ArrayList<UserInfo>") ArrayList<UserInfo> list) {
        return userInfoService.saveBatch(list);
    }


    @ApiOperation("根据ID更新")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public boolean putById(@ApiParam(value = "id要单独传") @PathVariable("id") Integer id, UserInfo userInfo) throws UnsupportedEncodingException {
        System.out.println("\n\n"+ userInfo+ "\n");
        if (!StringUtils.isEmpty(userInfo.getUsername())) {
            String username = userInfo.getUsername();
            username = Base64.encodeBase64String(username.getBytes("utf-8"));
            userInfo.setUsername(username);
        }
        return userInfoService.updateById(userInfo);
    }

    @ApiOperation("批量更新")
    @RequestMapping(value = "/_mupdate", method = RequestMethod.PUT)
    public boolean putBatches(@ApiParam(value = "传ArrayList<UserInfo>") ArrayList<UserInfo> list) {
        return userInfoService.updateBatchById(list);
    }


    @ApiOperation("根据ID删除")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public boolean deleteById(@PathVariable Integer id) {
        return userInfoService.removeById(id);
    }

    @ApiOperation("根据ID批量删除")
    @RequestMapping(value = "/_mdelete", method = RequestMethod.DELETE)
    public boolean deleteById(@ApiParam(value = "传ArrayList<Integer>") ArrayList<Integer> list) {
        return userInfoService.removeById(list);
    }

}
